<template>
	<view class="">
		<!-- 顶部导航 -->
		<fa-navbar title="编辑答案"></fa-navbar>
		<view class="u-p-30" v-if="isShow">
			<!-- #ifdef MP-WEIXIN || APP-PLUS || H5 -->
			<fa-editor ref="faEditor" :html="content_fmt" :isVoice="isVoice" :params="params"></fa-editor>
			<!-- #endif -->
			<!-- #ifdef MP-BAIDU || MP-ALIPAY -->
			<fa-markdown ref="faEditor" :markdown="content"></fa-markdown>
			<!-- #endif -->
			<view class="u-flex u-p-t-30" v-if="vuex_isAdmin">
				<view class="u-flex-6 u-p-r-10">
					<view class="u-p-b-20 text-bold">付费查看金额</view>
					<u-input v-model="price" type="number" :border="true" />
				</view>
				<view class="u-flex-6 u-p-l-10">
					<view class="u-p-b-20 text-bold">付费查看积分</view>
					<u-input v-model="score" type="number" :border="true" />
				</view>
			</view>
			<fa-captchaparts ref="captcha" label-position="left" :custom-style="{ paddingTop: '15rpx' }" mode="postanswer" v-model="captcha"></fa-captchaparts>
			<view class="u-p-30 u-flex u-row-between">
				<view class="u-flex-7">
					<u-button hover-class="none" :custom-style="{ backgroundColor: theme.bgColor, color: theme.color }" type="primary" @click="submit">提交保存</u-button>
				</view>
				<!-- #ifdef MP-WEIXIN || APP-PLUS -->
				<view class="u-m-l-15 u-flex-3">
					<u-button hover-class="none" :custom-style="{ backgroundColor: theme.bgColor, color: theme.color }" type="primary" @click="isVoice=!isVoice">
						{{isVoice?'文本回答':'语音回答'}}
					</u-button>					
				</view>
				<!-- #endif -->
			</view>
		</view>
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
	</view>
</template>

<script>
export default {
	onLoad(e) {
		let query = this.$Route.query || e || {};
		this.id = query.id || 0;
		this.index = query.index || 0;
		this.event = query.event || 'answerUpdate';
		this.getAnswerUpdate();
	},
	onHide() {
		uni.$emit('audioUpdate', '页面hide,停止');
	},
	data() {
		return {
			id: 0,
			index: 0, //上一个页面的数组索引
			event: 'answerUpdate', //事件名称
			content_fmt: '',
			content:'',
			price: 0,
			score: 0,
			isShow: false,
			isVoice: false,
			params: [],
			captcha:''
		};
	},
	methods: {
		getAnswerUpdate: async function() {
			let res = await this.$api.getAnswerUpdate({
				id: this.id
			});
			if (!res.code) {
				this.$u.toast(res.msg);
				setTimeout(() => {
					this.$wanlshop.back()
				}, 1500);
				return;
			}
			let content = res.data.content;
			let regExp = new RegExp("(http.*?(.mp3|.wav|.aac|.PCM))|(\\d+(\\.\\d+)?)",'g')
			let params = content.match(regExp);
			if (params && params.length == 2) {
				this.params = params;
				this.isVoice = true;
			} else {
				this.content_fmt = res.data.content_fmt;
				this.content = res.data.content;
			}
			this.isShow = true;
		},
		async submit() {
			// #ifdef H5
				if(this.isVoice){
					this.$u.toast('H5暂不支持语音录制！');
					return;
				}
			// #endif
			let content = await this.$refs.faEditor.getData();			
			let res = await this.$api.goAnswerUpdate({
				id: this.id,
				content: content,
				price: this.price,
				score: this.score,
				captcha:this.captcha
			});
			if (!res.code) {
				this.$u.toast(res.msg);
				this.$refs.captcha.getCaptcha();
				return;
			}
			//修改成功后发送更新好的事件
			uni.$emit(this.event, { index: this.index, data: res.data });
			this.$Router.back(1);
		}
	}
};
</script>

<style></style>
